/**
 * @param {number[]} nums
 * @return {number[][]}
 */
 var threeSum = function(nums) {
    const result=[];
    let n=nums.length;
    nums.sort(function(a,b){
        return a-b;
    })
    let start,end;
    for(let i=0;i<n-2;i++){
        start=i+1;
        end=n-1;
        if(nums[i]!=nums[i-1] || i==0){
            while(start<end){
                let sum=nums[i]+nums[start]+nums[end];
                if(sum==0){
                    result.push([nums[i],nums[start],nums[end]]);
                    start++;
                    end--;
                while(start<end && nums[start]==nums[start-1]){
                    start++;
                }
                while(start<end && nums[end]==nums[end+1]){
                    end--;
                }
                }
                else if(sum<0){
                    start++;
                }
                else{
                    end--;
                }
            }
        }
    }
    return result;
};
let s=[1,-1,-1,0];
console.log(threeSum(s))